angular.module('ui.menu', []).
    directive('menu', [function () {
        return {
            restrict: "C",
            link: function (b, c, e, d) {
                var href = e.uiSref;
                Menu.Instance().getData(function (data) {
                    var menus = [];
                    if (window.location.hash == '/login') {
                        return
                    }
                    if (data.Data) {
                        for (var i = 0; i < data.Data.length; i++) {
                            var item = data.Data[i];
                            menus.push(item.Url);
                        }
                        for (var i = 0; i < menus.length; i++) {
                            // if ((href).indexOf(menus[i]) != -1) {
                            if (href == menus[i]) {
                                e.$$element.removeClass('menu')
                                e.$$element.parents('.menuone').removeClass('none');

                            }
                        }

                    }

                });
            }
        }
    }]);
function Menu() {
}

Menu.Instance = function () {
    if (!Menu._init) {
        Menu.ins = new Menu();
        Menu._init = true;
    }
    return Menu.ins;
}

Menu.prototype.getData = function (func) {
    var uid = localStorage.getItem('u_id');
    var that = this;
    if (window.location.hash != '#/login') {
         if (!this.data) {
            this.data = true;
            var menuUrl = window.location.protocol+'//'+window.location.host
            $.get("http://192.168.0.109:9004/api/Staff/GetMenus", { u_id: uid }, function (data) {
                // $.get(menuUrl+"/api/Staff/GetMenus", { u_id: uid }, function (data) {
                if (data.Code == 1) {
                    that.data = data;
                    func(data);
                   
                } else if (data.Code == 8) {
                    window.location.hash = '/login'
                }
            })
        } else {
            if (this.data === true)
                setTimeout(function () {
                    that.getData(func);
                }, 200);
            else
                func(this.data);
        }
    }
}